module AuthorizedSystem
  protected
  
  def authorization_required
    authorized?(self.class.controller_path, action_name)
  end
  
  def authorized?(controller, action)
    unless current_user.roles.detect{|role|
        role.rights.detect{|right|
          (right.controller.nil? && right.action == action) || (right.action == action && right.controller == controller)}
      }
      return false
    end
    return true
  end
  
  def self.included(base)
    base.send :helper_method, :authorized?
  end
end